﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test07_UserMessage
    {
        private const string _userName = "engkeonglee";
        private const string _email = "engkeonglee@test.com";
        private const string _firstName = "Eng-Keong";
        private const string _lastName = "Lee";
        private string[] _roles = new string[1] { "User" };

        [TestFixtureSetUp()]
        public void Start()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    BusinessRulePreferences.Username userName = new BusinessRulePreferences.Username();
                    userName.SetSelectedBusinessRule(session, "Simple username");

                    User user;
                    for (int i = 0; i < 3; i++)
                    {
                        user = User.GetUserByUsername(session, _userName + i.ToString());
                        if (user != null)
                        {
                            user.Delete(session);
                        }
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        User.NewUser(session, _firstName + i.ToString(), _lastName + i.ToString(), 
                            _userName + i.ToString(), _email + i.ToString(), Guid.NewGuid().ToString(), _roles);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test01_SendMessageFrom0To1()
        {
            using (var session = DataProvider.Instance.Session)
            {
                User user = User.GetUserByUsername(session, _userName + "0");
                Assert.IsNotNull(user);
                using (var transaction = session.BeginTransaction())
                {
                    user.Send(session, _userName + "1", "Test Subject 0", "Hello 0!");

                    User user0 = User.GetUserByUsername(session, _userName + "0");
                    Assert.AreEqual(user0.SentMessages.Count, 1);
                    Message user0SentMessage = user0.SentMessages[0];
                    Assert.AreEqual(user0SentMessage.Subject, "Test Subject 0");
                    Assert.AreEqual(user0SentMessage.Description, "Hello 0!");

                    User user1 = User.GetUserByUsername(session, _userName + "1");
                    bool found = false;
                    IList<Message> messages = user1.GetNewMessages(session);
                    for (int i = 0; i < messages.Count; i++)
                    {
                        Message message = messages[i];
                        if (message.Subject == "Test Subject 0")
                        {
                            Assert.AreEqual("Hello 0!", message.Description);
                            found = true;
                        }
                        message.Opened = DateTime.Now;
                        message.Save(session);
                    }
                    Assert.IsTrue(found);

                    messages = user1.GetNewMessages(session);
                    Assert.AreEqual(0, messages.Count);

                    transaction.Commit();
                    transaction.Dispose();
                }
                session.Close();
                session.Dispose();
            }
        }
    }
}
